package com.example.wechattakeaway.service.Impl;

import com.example.wechattakeaway.entity.Merchant;
import com.example.wechattakeaway.enums.ResultEnum;
import com.example.wechattakeaway.form.merchantform.InsertMerchantForm;
import com.example.wechattakeaway.form.merchantform.UpdateMerchantForm;
import com.example.wechattakeaway.mapper.AttentionMapper;
import com.example.wechattakeaway.mapper.MerOrderMapper;
import com.example.wechattakeaway.mapper.MerchantMapper;
import com.example.wechattakeaway.service.MerchantService;
import com.example.wechattakeaway.utils.FileUpload;
import com.example.wechattakeaway.utils.ResultVOUtil;
import com.example.wechattakeaway.vo.ResultVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 商家信息管理服务层实现类
 */
@Slf4j
@Service
@Repository
public class MerchantServiceImpl implements MerchantService {

    @Autowired
    MerchantMapper merchantMapper;

    @Autowired
    MerOrderMapper merOrderMapper;

    @Autowired
    AttentionMapper attentionMapper;
    /**
     * 添加商家信息
     * @param insertMerchantForm
     * @return
     */

    @Override
    public ResultVO insertMerchant(InsertMerchantForm insertMerchantForm) {
        Merchant merchant=new Merchant();
        merchant.setDescription(insertMerchantForm.getDescription());
        merchant.setImgUrl(insertMerchantForm.getImgUrl());
        merchant.setMerchantAddress(insertMerchantForm.getMerchantAddress());
        merchant.setMerchantPhone(insertMerchantForm.getMerchantPhone());
        merchant.setMerchantType(insertMerchantForm.getMerchantType());
        merchant.setMerchantName(insertMerchantForm.getMerchantName());
        merchant.setUserId(insertMerchantForm.getUserId());


      //  System.out.println(merchant.getUserId()+"merchantURL------------------------");
        try {

            merchantMapper.insert(merchant);

        }catch (Exception e){
            e.printStackTrace();
            return ResultVOUtil.error(ResultEnum.NORMAL_ERROR);
        }


        return ResultVOUtil.success();
    }

    /**
     * 编辑商家信息
     * @param updateMerchantForm
     * @return
     */
    @Override
    public ResultVO updateMerchant(UpdateMerchantForm updateMerchantForm) {
        Merchant merchant=new Merchant();
        merchant.setMerchantId(updateMerchantForm.getMerchantId());
        merchant.setDescription(updateMerchantForm.getDescription());
        merchant.setMerchantName(updateMerchantForm.getMerchantName());
        merchant.setMerchantPhone(updateMerchantForm.getMerchantPhone());
        merchant.setMerchantType(updateMerchantForm.getMerchantType());
        merchant.setImgUrl(updateMerchantForm.getImgUrl());

        try {
            merchantMapper.updateByPrimaryKeySelective(merchant);

        }catch (Exception e){
            e.printStackTrace();
            return ResultVOUtil.error(ResultEnum.NORMAL_ERROR);
        }

        return ResultVOUtil.success();

    }

    /**
     * 查询所有商家信息列表
     * @return
     */
    @Override
    public ResultVO selectAllMerchant() {
        List<Merchant> list=new ArrayList<>();

        try {
          //  PageHelper.startPage(pageNum,5);
            list=merchantMapper.selectAll();

        }catch (Exception e){
            e.printStackTrace();
            return ResultVOUtil.error(ResultEnum.NORMAL_ERROR);
        }
        HashMap<String,Object> hashMap = new HashMap<>();
       // hashMap = PageUtil.getMap(list);
        hashMap.put("list",list);
        return ResultVOUtil.success(hashMap);
    }

    /**
     * 根据id查询商家信息
     * @param merchantId
     * @return
     */
    @Override
    public ResultVO selectMerchantById(Integer merchantId) {
        Merchant merchant=new Merchant();

        try{
            merchant = merchantMapper.selectByPrimaryKey(merchantId);
        }catch (Exception e){
            e.printStackTrace();
            return ResultVOUtil.error(ResultEnum.NORMAL_ERROR);
        }
        return ResultVOUtil.success(merchant);
    }

    @Override
    public ResultVO deleteMerchant(Integer merchantId) {
        try{
            merchantMapper.deleteByPrimaryKey(merchantId);
        }catch (Exception e){
            e.printStackTrace();
            return ResultVOUtil.error(ResultEnum.NORMAL_ERROR);
        }
        return ResultVOUtil.success();
    }

    /**
     * 根据商家类型查询对应商家列表
     * @param merchantType
     * @return
     */

    @Override
    public ResultVO selectMerchantByType(String merchantType) {
        List<Merchant> list=new ArrayList<>();

        try {
          //  PageHelper.startPage(pageNum,5);
            list=merchantMapper.selectByType(merchantType);

        }catch (Exception e){
            e.printStackTrace();
            return ResultVOUtil.error(ResultEnum.NORMAL_ERROR);
        }
        HashMap<String,Object> hashMap = new HashMap<>();
       // hashMap = PageUtil.getMap(list);
        hashMap.put("list",list);
        return ResultVOUtil.success(hashMap);
    }

    /**
     * 根据订单量降序排列
     * @return
     */
    @Override
    public ResultVO selectByOrderNum() {

       // PageHelper.startPage(pageNum,5);
        List<Merchant> merchants=new ArrayList<>();
        List<Integer> ids= merOrderMapper.SelectMerchantByOrderNum();
        //  List<Integer> ids= Lists.newArrayList(19,18,16,12);
        System.out.println(ids+"ids-----------------ordernum");
        try {
            for(int i=0;i<ids.size();i++){
                Merchant merchant=new Merchant();

                merchant = merchantMapper.selectByPrimaryKey(ids.get(i));
                merchants.add(merchant);
            }

        }catch (Exception e){
            e.printStackTrace();
            return ResultVOUtil.error(ResultEnum.NORMAL_ERROR);
        }

        HashMap<String,Object> hashMap = new HashMap<>();
        //hashMap = PageUtil.getMap(merchants);
        hashMap.put("list",merchants);
        return ResultVOUtil.success(hashMap);
    }

    /**
     * 根据商家类型，以及订单数量降序返回商家id列表
     * @param merchantType
     * @return
     */
    @Override
    public ResultVO selectByTypeAndOrder(String merchantType) {

//        PageHelper.startPage(pageNum,5);
        List<Merchant> merchants=new ArrayList<>();
        List<Integer> ids= merOrderMapper.SelectByOrderNumAndType(merchantType);
//        System.out.println(ids+"ids-----------------ordernumandType");
//        System.out.println(ids.size()+"ids.size-----------------ordernumandType");
//        System.out.println(ids.get(0)+"ids.get(0)-----------------ordernumandType");
        try {
            for(int i=0;i<ids.size();i++){
                Merchant merchant=new Merchant();
                merchant=merchantMapper.selectByPrimaryKey(ids.get(i));
                merchants.add(merchant);
            }

        }catch (Exception e){
            e.printStackTrace();
            return ResultVOUtil.error(ResultEnum.NORMAL_ERROR);
        }

        HashMap<String,Object> hashMap = new HashMap<>();
        //hashMap = PageUtil.getMap(merchants);
        hashMap.put("list",merchants);
        return ResultVOUtil.success(hashMap);
    }

    /**
     * 在进入分类之后，根据商家被关注数量（降序）查询对应商家列表
     * @param merchantType
     * @return
     */
    @Override
    public ResultVO selectByAttenAndType(String merchantType) {

      //  PageHelper.startPage(pageNum,5);

        List<Merchant> merchants=new ArrayList<>();
        List<Integer> ids= attentionMapper.ShowMerByAttentionAndType(merchantType);
        try {
            for(int i=0;i<ids.size();i++){
                Merchant merchant = new Merchant();
                merchant = merchantMapper.selectByPrimaryKey(ids.get(i));
                merchants.add(merchant);
            }

        }catch (Exception e){
            e.printStackTrace();
            return ResultVOUtil.error(ResultEnum.NORMAL_ERROR);
        }

        HashMap<String,Object> hashMap = new HashMap<>();
      //  hashMap = PageUtil.getMap(merchants);
        hashMap.put("list",merchants);
        return ResultVOUtil.success(hashMap);
    }

    /**
     * 首页根据商家被关注数量（降序）查询对应商家列表
     * @return
     */
    @Override
    public ResultVO selectByAttenNum() {
//        PageHelper.startPage(pageNum,5);

        List<Merchant> merchants=new ArrayList<>();
        List<Integer> ids= attentionMapper.ShowMerByAttentionNum();
        try {
            for(int i=0;i<ids.size();i++){
                Merchant merchant=new Merchant();
                merchant=merchantMapper.selectByPrimaryKey(ids.get(i));
                merchants.add(merchant);
            }

        }catch (Exception e){
            e.printStackTrace();
            return ResultVOUtil.error(ResultEnum.NORMAL_ERROR);
        }

        HashMap<String,Object> hashMap = new HashMap<>();
//        hashMap = PageUtil.getMap(merchants);
        hashMap.put("list",merchants);
        return ResultVOUtil.success(hashMap);
    }

    @Override
    public ResultVO selectMerByUser(Integer userId) {
        Merchant merchant = new Merchant();
        try{
            merchant = merchantMapper.selectMerByUser(userId);
        }catch (Exception e){
            e.printStackTrace();
            return ResultVOUtil.error(ResultEnum.NORMAL_ERROR);
        }
        return ResultVOUtil.success(merchant);
    }

    @Override
    public ResultVO UpdateImgById(MultipartFile file, Integer merchantId) {
        String fileName = FileUpload.UploadPicture(file);
        try{
            merchantMapper.UpdateImgById(fileName,merchantId);
        }catch (Exception e){
            e.printStackTrace();
            return ResultVOUtil.error(ResultEnum.NORMAL_ERROR);
        }
        return ResultVOUtil.success();
    }


//    /**
//     * 上传商家图片
//     * @param file
//     * @param merchantId
//     * @return
//     */
//    @Override
//    public ResultVO UploadMerchantImg(MultipartFile file, Integer merchantId) {
//        String imgUrl= FileUpload.UploadPicture(file);
//        Merchant merchant=new Merchant();
//        try{
//            merchantMapper.UploadMerchantImg(imgUrl,merchantId);
//        }catch (Exception e){
//            e.printStackTrace();
//            return ResultVOUtil.error(ResultEnum.NORMAL_ERROR);
//        }
//        return ResultVOUtil.success();
//
//    }
}
